/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.iussoft.portal.tags
 */
package com.apache.portal.tags;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.apache.api.vo.ResultEntity;
import com.apache.client.UctCoreClient;
import com.apache.portal.common.PortalPlugin;
import com.apache.portal.common.annotion.BeanFactory;
import com.apache.tools.StrUtil;
import com.apache.uct.common.AuthUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.entity.User;

/**
 * description:  用户中心权限标签
 * @author Hou Dayu 创建时间：2016-8-24
 */
@SuppressWarnings("serial")
public class UctPortalTag extends TagSupport {
	private String actFullEname;
	private String lockType;
	private String userId;
	private String userEname;

	public int doStartTag() throws JspException {
		boolean b = false;
		try {
			HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
			if (StrUtil.isNotNull(userEname) || StrUtil.isNotNull(userId)) {//获取用户信息
				Map<String, String> params = new HashMap<String, String>();
				params.put("userId", userId);
				params.put("userEname", userEname);
				params.put("doCode", "U0007");
				Object obj = ((PortalPlugin) BeanFactory.getInstance().getBeans("uct")).doInvoke(request, null, params);
				request.setAttribute("userInfo", obj);
			} else {
				LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
				if ("bank".equalsIgnoreCase(lockType)) {
					ResultEntity entity = UctCoreClient.getUserByProperty("userId", loginUser.getUserId());
					loginUser.setUser((User) entity.getEntity());
					request.getSession().setAttribute("loginUser", loginUser);
				} else {
					if (loginUser != null) {
						if ("strictLock".equalsIgnoreCase(lockType)) {
							b = AuthUtil.openStrictLock(actFullEname, loginUser);
						} else if ("childLock".equalsIgnoreCase(lockType)) {
							b = AuthUtil.openChildLock(actFullEname, loginUser);
						} else if ("famillyLock".equalsIgnoreCase(lockType)) {
							b = AuthUtil.openFamillyLock(actFullEname, loginUser);
						} else if ("fatherLock".equalsIgnoreCase(lockType)) {
							b = AuthUtil.openFatherLock(actFullEname, loginUser);
						} else if ("sysLock".equalsIgnoreCase(lockType)) {
							String str[] = actFullEname.split("-");
							b = str[0].indexOf(str[1]) != -1;//验证是否有系统权限
						}
					}
				}
			}
		} catch (Exception e) {
		}
		if (b) {
			return super.EVAL_BODY_INCLUDE;
		} else {
			return super.SKIP_BODY;
		}
	}

	public int doEndTag() throws JspException {
		return super.EVAL_PAGE;
	}

	public String getActFullEname() {
		return actFullEname;
	}

	public void setActFullEname(String actFullEname) {
		this.actFullEname = actFullEname;
	}

	public String getLockType() {
		return lockType;
	}

	public void setLockType(String lockType) {
		this.lockType = lockType;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserEname() {
		return userEname;
	}

	public void setUserEname(String userEname) {
		this.userEname = userEname;
	}
}
